//
//  AppDelegate.m
//  CT
//
//  Created by liufanwei on 15/3/13.
//  Copyright (c) 2015年 liufanwei. All rights reserved.
//

#import "AppDelegate.h"
#import "CoreData+MagicalRecord.h"
#import "CTUserDefaultHelper.h"
#import "CommonMicro.h"
#import "CTDataManager.h"

@interface AppDelegate ()

@end

@implementation AppDelegate
static NSString * const kRecipesStoreName = @"CT.sqlite";


- (void) copyDefaultStoreIfNecessary;
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    
    NSString *storePath = [[NSPersistentStore MR_urlForStoreName:kRecipesStoreName] path];
    
    // If the expected store doesn't exist, copy the default store.
    if (![fileManager fileExistsAtPath:storePath])
    {
        NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:[kRecipesStoreName stringByDeletingPathExtension] ofType:[kRecipesStoreName pathExtension]];
        
        if (defaultStorePath){
            BOOL success = [fileManager copyItemAtPath:defaultStorePath toPath:storePath error:nil];
            if (!success){
                NSLog(@"Failed to install default recipe store");
            }
        }
    }
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // 初始化数据库
    [self copyDefaultStoreIfNecessary];
    [MagicalRecord setLoggingLevel:MagicalRecordLoggingLevelVerbose];
    [MagicalRecord setupCoreDataStackWithStoreNamed:kRecipesStoreName];
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.window makeKeyAndVisible];
    
    [CTDataManager shareInstance];
    NSString * account = [[NSUserDefaults standardUserDefaults] objectForKey:kAccount];
    if (!account) {
         [self ShowLogin];
    }else{
        [self ShowMain];
    }
    return YES;
}

-(void)ShowLogin{
    UIStoryboard* mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UITabBarController *tbVC = [mainStoryboard instantiateViewControllerWithIdentifier:@"loginnavigationcontroller"];
    self.window.rootViewController = tbVC;
}

-(void)ShowMain{
    UIStoryboard* mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UITabBarController *tbVC = [mainStoryboard instantiateViewControllerWithIdentifier:@"mainTabBarController"];
    [self.window setRootViewController:tbVC];
}

- (void)applicationWillResignActive:(UIApplication *)application {
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    [[NSNotificationCenter defaultCenter] postNotificationName:kNotifiCationLoadContactsRequest object:Nil];
}

- (void)applicationWillTerminate:(UIApplication *)application {
}

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    NSUInteger orientations = UIInterfaceOrientationMaskAllButUpsideDown;
    
    if(self.window.rootViewController){
        UIViewController *presentedViewController = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
        orientations = [presentedViewController supportedInterfaceOrientations];
    }
    
    return UIInterfaceOrientationMaskPortrait;
}

@end
